package com.standardoperationapp.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.standardoperationapp.R;
import com.standardoperationapp.bean.CilckEventBean;
import com.standardoperationapp.bean.LockOpenBean;
import com.standardoperationapp.global.GlobalVaries;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class DoorListAdapter extends BaseAdapter {
    private List<LockOpenBean.DataItem> list;
    private LayoutInflater mInflater;

    public DoorListAdapter(Context context, List<LockOpenBean.DataItem> list1) {
        list = list1;
        mInflater = LayoutInflater.from(context);
    }



    public int getCount() {
        return list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public int getItemViewType(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        LockOpenBean.DataItem item = list.get(position);
      ViewHolder viewHolder = null;

        if(convertView == null){
            convertView = mInflater.inflate(R.layout.door_list_item, null);
            viewHolder=new ViewHolder(
                    (View) convertView.findViewById(R.id.door_list_child),
                    (TextView) convertView.findViewById(R.id.door_txt1),
                    (TextView) convertView.findViewById(R.id.door_txt2),
                    (TextView) convertView.findViewById(R.id.door_txt3),
                    (TextView) convertView.findViewById(R.id.door_txt4)


            );
            convertView.setTag(viewHolder);
        }
        else{
            viewHolder = (ViewHolder)convertView.getTag();
        }

        viewHolder.tx1.setText(item.getDoorName());
        viewHolder.tx2.setText(item.getResidueOpenLockCount().toString());
        viewHolder.tx3.setText(item.getLeftOpenLockTime()+"\n"+item.getRightOpenLockTime());
        viewHolder.tx4.setText("开锁");
        viewHolder.tx4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GlobalVaries.curBlueMac =item.getMacAddress();
                EventBus.getDefault().post(new CilckEventBean(position,1));
            }
        });
        if ((position % 2) == 1) {
            viewHolder.child.setBackgroundColor(Color.parseColor("#F1F1F1"));
        } else {
            viewHolder.child.setBackgroundColor(Color.WHITE);
        }

//        {
//            viewHolder.child.setBackgroundResource(R.drawable.msgbox_send);
//            viewHolder.msgTextView.setText(item.getComItem());
//        }
//        else
//        {
//            viewHolder.child.setBackgroundResource(R.drawable.msgbox_rec);
//            viewHolder.msgTextView.setText(item.getComItem());
//        }


        return convertView;
    }

    public void upDta(List<LockOpenBean.DataItem> list) {
        this.list = list;
    }

    class ViewHolder
    {
        protected TextView tx1,tx2,tx3,tx4;
        protected View child;


        public ViewHolder(View child, TextView tx1,TextView tx2,TextView tx3,TextView tx4){
            this.child = child;
            this.tx1 = tx1;
            this.tx2 =tx2;
            this.tx3 = tx3;
            this.tx4=tx4;



        }
    }
}
